package cn.hwua.web;

import cn.hwua.dao.NewsDao;
import cn.hwua.dao.impl.NewsDaoImpl;
import cn.hwua.entity.News;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/index/*")
public class IndexServlet extends HttpServlet {
    private NewsDao dao = new NewsDaoImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String baseURL = req.getContextPath();//获取项目的基本路径
        String uri = req.getRequestURI();
        String path = uri.substring(uri.lastIndexOf("/") + 1);
        if ("list.do".equals(path)){
            List<News> newsList = dao.reading();
            req.setAttribute("newsList",newsList);
            req.getRequestDispatcher(baseURL+"/pc/index.jsp").forward(req,resp);
            return;
        } else {
            throw new RuntimeException("请求错误");
        }
    }
}
